//
//  SEGEncryptRule.m
//  UhomebkSDK
//
//  Created by wangmm on 2019/3/19.
//  Copyright © 2019年 wangmm. All rights reserved.
//

#import "SEGEncryptRule.h"
#import "SEGRSA.h"
#import "NSString+SEGSecurity.h"

#if __has_include(<Commom/NSString+YYAdd.h>)
#import <Commom/NSString+YYAdd.h>
#else
#import "NSString+YYAdd.h"
#endif

#import "MJEncryptStringData.h"
#import "MJEncryptString.h"

#define rsaPublicKey mj_OCString(_3130729332)

static NSString * _getxxx(void) {
    return rsaPublicKey;
}

static NSString * _handleString(NSString *string) {
    NSString *base64Encrypt = [string base64EncodedString];
    NSString *rsaString = [SEGRSA encryptString:base64Encrypt publicKey:_getxxx()];
    return rsaString;
}

static XXUtil_t * util = NULL;

@implementation _XXUtil
+(XXUtil_t *)sharedUtil
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        util = malloc(sizeof(XXUtil_t));
        util->getxxx = _getxxx;
        util->handleString = _handleString;
    });
    return util;
}
+ (void)destroy
{
    util ? free(util): 0;
    util = NULL;
}
@end

@implementation SEGEncryptRule

+ (NSString *)encryptByRsaWithString:(NSString *)string
{
//    NSString *base64Encrypt = [string base64EncodedString];
//    NSString *rsaString = [SEGRSA encryptString:base64Encrypt publicKey:rsaPublicKey];
//    return rsaString;

    return XXUtil->handleString(string);
}

//+ (NSString *)encryptLoginByUsername:(NSString *)username Password:(NSString *)password andSecurityKey:(NSString *)securityKey andPublicKey:(NSString *)publicKey
//{
//    //1.使用公钥（publicKey后台下发）将密码rsa加密。
//    NSString * rsaPassword = [SEGRSA encryptString:password publicKey:publicKey];
//    //2.按照key首字母升序拼接密码和用户名
//    NSString * passwordANDusername = [NSString stringWithFormat:@"password=%@&username=%@",rsaPassword,username];
//    //3.三十二位小写md5
//    NSString * passwordANDusernameMD5 = [NSString seg_md532BitLower:passwordANDusername];
//    //4.再加上密钥（securityKey后台下发）
//    NSString * sign = [NSString stringWithFormat:@"%@%@",passwordANDusernameMD5,securityKey];
//    //5.三十二位小写md5
//    sign = [NSString seg_md532BitLower:sign];
//    //6.网络访问头部需要加上sign值，eg:[[SEGSDKHTTPManager sharedManager] httpHeaderWithOtherDict:@{@"sign":sign}];
//    //7.传给后台的密码需要rsaPassword的值,不能明文,用户名是明文
//    return sign;
//}

@end
